package com.ely.coupon.distribute.controller;

import com.alicp.jetcache.anno.Cached;
import com.ely.coupon.common.core.result.ResponseResult;
import com.ely.coupon.common.lock.DistributedLockedAop;
import com.ely.coupon.common.lock.annotation.DistributedLocked;
import com.ely.coupon.common.lock.enums.LockType;
import com.ely.coupon.common.lock.enums.ReadWriteLockType;
import com.ely.coupon.distribute.service.CouponDistributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
//  全局设置    系统预热
//

/**
 * 缓存雪崩  --->   大量key同时过期     单线程     解决高并发
 * <p>
 * 自动发发  会员等级  v10  会员ID
 */
@RestController
@RequestMapping("/coupon/distribute")
@Api(tags = "优惠券分发系统Api接口文档")
public class CouponDistributeController {
    @Resource
    private CouponDistributeService couponDistributeService;

    /*
     * 用户主动领取优惠券  业务逻辑
     */
    @Cached(expire = 600, key = "#{page},#{size}")
    public ResponseResult<String> list(Integer page, Integer size) {
        return ResponseResult.success("");
    }

    /**
     * 登录    会员ID
     *
     * @return
     */
    @ApiOperation("领取优惠券")
    @ApiImplicitParams({@ApiImplicitParam(name = "memberId", value = "会员ID"), @ApiImplicitParam(name = "couponActivityId", value = "优惠券活动ID")})
    @PostMapping("/receive")
    @DistributedLocked(value = "coupon:receive", type = LockType.READ_WRITE_LOCK, readWriteType = ReadWriteLockType.READ_LOCK, waitTime = 30, leaseTime = 8, unit = TimeUnit.SECONDS)
    public ResponseResult<Integer> receive(@RequestParam Long memberId, @RequestParam Long couponActivityId) {
        return ResponseResult.success(couponDistributeService.receive(memberId, couponActivityId));
    }

}

